package dashboard.databll;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


import dashboard.cache.ServiceBeanFactory;
import dashboard.constant.IncomeCityConst;
import dashboard.datasource.AbstractBaseService;
import dashboard.dbentity.IncomeCity;
import dashboard.dbentity.IncomeCityConsume;
import dashboard.utils.DateUtil;

public class IncomeBLL {
	private static AbstractBaseService<IncomeCity> icmCityService = ServiceBeanFactory.get(IncomeCity.class,null);
	private static AbstractBaseService<IncomeCityConsume> icmCityConsumeService = ServiceBeanFactory.get(IncomeCityConsume.class,null);
	
	/**
	 * 城市对应的产品(精准，置顶，智能)趋势图数据
	 */
	public static String getCityProJson(String dateGroup, String stat_type, String businessName, String cityName){
		String fields[] ={"jingzhun_cash","jingzhun_cus","zhiding_cash","zhiding_cus","zhineng_cash","zhineng_cus"};
//		String cols = IncomeCityConst.getCityProCol(dateGroup);
//		String condition =  IncomeCityConst.getCityProCon(dateGroup, stat_type, businessName, cityName);
//		List<IncomeCity> list = icmCityService.getListByConditon( cols, condition, dateGroup);
//		TreeMap<String, List<IncomeCity>> stat_dateMap = new TreeMap<String, List<IncomeCity>>(ReflectUtil.groupByField(list,dateGroup));

		HashMap<String, List<Number>> dataMap = new HashMap<String, List<Number>>(); // stat_date : [dataList]
//		for (String date : stat_dateMap.keySet()) {
//			IncomeCity cityVo = stat_dateMap.get(date).get(0);
//			for (String f : fields) {
//				List<Number> curList = dataMap.get(f);
//				if(curList == null)
//					curList = new ArrayList<Number>();
//				curList.add((Number) ReflectUtil.getVal(cityVo, f));
//				dataMap.put(f, curList);
//			}
//		}

		JSONObject ob = new JSONObject();
		List<String> dayList = DateUtil.getDateList("2015-03-01", "2015-05-04");
		for (String f : fields) {
			List<Number> dataList = new ArrayList<Number>();
			for(int i=0;i<dayList.size();i++){
				dataList.add(Math.round(Math.random()*1000000));
			}
			dataMap.put(f, dataList);
		}
//		ob.element("time", stat_dateMap.keySet());
		ob.element("time", dayList);
		for(String p:dataMap.keySet()){
			ob.element(p, dataMap.get(p));
		}

		return ob.toString();
	}
		
	/**
	 * echarts点击获取城市对应的产品(精准，置顶，智能) 表格展示数据
	 */
	public static String getCityProTableJson(String dateGroup, String stat_type, String businessName, String dateVal){
//		String fields[] ={"jingzhun_cash","jingzhun_cus","zhiding_cash","zhiding_cus","zhineng_cash","zhineng_cus"};
//		String condition =  IncomeCityConst.getCityProTableCon(dateGroup, stat_type, businessName, dateVal);
//		List<IncomeCity> list = icmCityService.getListByConditon( null, condition, dateGroup);

		JSONObject ob = new JSONObject();
		List<IncomeCity> list =new ArrayList<IncomeCity>();
		for (Object f : IncomeCityConst.ZX_CityList) {
			IncomeCity voCity = new IncomeCity(dateVal, stat_type, businessName,f.toString(),
					(int)Math.round(Math.random()*1000000), (float)Math.random(), (int)Math.round(Math.random()*1000000), 
					(int)Math.round(Math.random()*1000000), (float)Math.random(),(int)Math.round(Math.random()*1000000), 
					(int)Math.round(Math.random()*1000000),(float)Math.random(), (int)Math.round(Math.random()*1000000),
					(int)Math.round(Math.random()*1000000),(float) Math.random(), (float)Math.random());
			list.add(voCity);
		}
		ob.put("dataList", list);

		return ob.toString();
	}
	
	/**
	 * 产品(精准|置顶|智能)的城市对比趋势图数据
	 */
	public static String getCityCmpJson(String dateGroup, String stat_type, String businessName,String proType, String city1, String city2){
		String[] citys={city1,city2};
		String fields[] ={proType+"_cash",proType+"_cus"};
//		String cols = IncomeCityConst.getCityCmpCol(dateGroup, proType);
		String condition =  IncomeCityConst.getCityCmpCon(dateGroup, stat_type, businessName,city1,city2);
//		List<IncomeCity> list = icmCityService.getListByConditon( cols, condition, null);
//		TreeMap<String, List<IncomeCity>> stat_dateMap = new TreeMap<String, List<IncomeCity>>(ReflectUtil.groupByField(list,dateGroup));

		HashMap<String, List<Number>> dataMap = new HashMap<String, List<Number>>(); // key : [dataList]
//		for (String date : stat_dateMap.keySet()) {
//			for (String ct : citys) {
//				IncomeCity cityVo = ReflectUtil.getByColVal(IncomeCity.class,stat_dateMap.get(date),"city_name",ct);
//				for (String f : fields) {
//					String key = ct+"_"+f;
//					List<Number> curList = dataMap.get(key);
//					if(curList == null)
//						curList = new ArrayList<Number>();
//					curList.add((Number) ReflectUtil.getVal(cityVo, f));
//					dataMap.put(key, curList);
//				}
//			}
//		}

		JSONObject ob = new JSONObject();
//		ob.element("time", stat_dateMap.keySet());
		
		List<String> dayList = DateUtil.getDateList("2015-03-01", "2015-05-04");

		for(int i=0;i<dayList.size();i++){
			for (String ct : citys) {
				for (String f : fields) {
					String key = ct+"_"+f;
					List<Number> curList = dataMap.get(key);
					if(curList == null)
						curList = new ArrayList<Number>();
					curList.add(Math.round(Math.random()*1000000));
					dataMap.put(key, curList);
				}
			}
		}
		ob.element("time", dayList);
		for(String p:dataMap.keySet()){
			ob.element(p, dataMap.get(p));
		}

		return ob.toString();
	}

	/**
	 * 城市消耗周期
	 */
	public static String getCityConsumeJson(String dateGroup, String stat_type,
			String businessName, String consume_citys, String index1, String index2) {
		
		HashMap<String, List<Number>> dataMap = new HashMap<String, List<Number>>(); // key : [dataList]
		String[] indexs  = {index1,index2};
		JSONArray cityArr = JSONArray.fromObject(consume_citys.split(","));
		
//		String cols = IncomeCityConst.getCityConsumeCol(dateGroup, index1,index2);
//		String condition =  IncomeCityConst.getCityConsumeCon(dateGroup, stat_type, businessName,cityArr);
//		List<IncomeCity> list = icmCityConsumeService.getListByConditon( cols, condition, null);
//		TreeMap<String, List<IncomeCity>> stat_dateMap = new TreeMap<String, List<IncomeCity>>(ReflectUtil.groupByField(list,dateGroup));
//		for (String date : stat_dateMap.keySet()) {
//			for (Object ct : cityArr) {
//				IncomeCity cityVo = ReflectUtil.getByColVal(IncomeCity.class,stat_dateMap.get(date),"city_name",ct);
//				for (String f : indexs) {
//					String key = ct+"_"+f;
//					List<Number> curList = dataMap.get(key);
//					if(curList == null)
//						curList = new ArrayList<Number>();
//					curList.add((Number) ReflectUtil.getVal(cityVo, f));
//					dataMap.put(key, curList);
//				}
//			}
//		}

		JSONObject ob = new JSONObject();
//		ob.element("time", stat_dateMap.keySet());
		
		List<String> dayList = DateUtil.getDateList("2015-03-01", "2015-05-04");

		for(int i=0;i<dayList.size();i++){
			for (Object ct : cityArr) {
				for (String f : indexs) {
					String key = ct+"_"+f;
					List<Number> curList = dataMap.get(key);
					if(curList == null)
						curList = new ArrayList<Number>();
					if(StringUtils.equals(f, "xhzq"))
						curList.add(Math.round(Math.random()*100));
					else
						curList.add(Math.round(Math.random()*1000000));
					dataMap.put(key, curList);
				}
			}
		}
		ob.element("time", dayList);
		for(String p:dataMap.keySet()){
			ob.element(p, dataMap.get(p));
		}

		return ob.toString();
	}
	
	
	/**
	 * echarts点击获取  城市消耗周期 表格展示数据
	 */
	public static String getCityConsumeTableJson(String dateGroup, String stat_type, String businessName, String dateVal){
//		String condition =  IncomeCityConst.getCityProTableCon(dateGroup, stat_type, businessName, dateVal);
//		List<IncomeCityConsume> list = icmCityConsumeService.getListByConditon( null, condition, null);

		JSONObject ob = new JSONObject();
		List<IncomeCityConsume> list =new ArrayList<IncomeCityConsume>();
		for (Object f : IncomeCityConst.ZX_CityList) {
			IncomeCityConsume voCity = new IncomeCityConsume(dateVal, stat_type, businessName,f.toString(),
					(int)Math.round(Math.random()*1000000), (float)Math.random(),  
					(int)Math.round(Math.random()*1000000), (float)Math.random(),
					(int)Math.round(Math.random()*1000000),(float)Math.random(), 
					(int)Math.round(Math.random()*1000000),(float) Math.random());
			list.add(voCity);
		}
		ob.put("dataList", list);

		return ob.toString();
	}
	
	
}
